package com.carlinfo.shop.common.util;

import java.io.File;
import java.util.Date;

import javax.annotation.Resource;
import javax.mail.Message.RecipientType;
import javax.mail.internet.MimeMessage;

import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Component;

/**
 * Spring发送邮件的工具类
 * 
 * @author wangshMac
 *
 */
@Component("springEmailUtil")
public class SpringEmailUtil
{
	/* 邮局 */
	@Resource
	private JavaMailSender javaMailSender;
	/* 信封 */
	@Resource
	private SimpleMailMessage mailMessage;

	/**
	 * 发送邮件
	 * 
	 * @param to
	 *            收件人
	 * @param subject
	 *            邮件的标题
	 * @param text
	 *            邮件的内容(只能发送纯文本)
	 * @return true:发送成功,false:发送失败
	 */
	public boolean send(String to, String subject, String text)
	{
		ConstatFinalUtil.LOGGER.info("==send=");
		try
		{
			/* 写信的过程 */
			this.mailMessage.setTo(to);
			this.mailMessage.setSubject(subject);
			this.mailMessage.setText(text);
			this.mailMessage.setSentDate(new Date());

			/* 需要引入java-mail的jar包 */
			/*
			 * 如何知道邮件是不是发送成功呢? 只能以是否抛出异常做为判断了
			 */
			this.javaMailSender.send(mailMessage);
			return true;
		} catch (Exception e)
		{
			ConstatFinalUtil.LOGGER.info("发送邮件失败了;收件人:{},邮件的标题:{}", to, subject, e);
		}
		return false;
	}
	
	/**
	 * 发送html邮件
	 * @param to
	 * @param subject
	 * @param text
	 * @return
	 */
	public boolean sendHTML(String to, String subject, String text)
	{
		ConstatFinalUtil.LOGGER.info("==sendHTML=");
		try
		{
			/* 获取发件人 */
			String from = this.mailMessage.getFrom(); 
			MimeMessage mimeMessage = this.javaMailSender.createMimeMessage();
			/* 创建一个html邮件 */
			MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage,true,"UTF-8");
			messageHelper.setFrom(from);
			messageHelper.setTo(to);
			messageHelper.setSubject(subject);
			messageHelper.setText(text, true);
			messageHelper.setSentDate(new Date());
			
			/* 发送附件 */
			/*FileSystemResource res = new FileSystemResource(new File("C:\\Users\\wangshMac\\Pictures\\QQ浏览器截图\\1.png"));
			messageHelper.addInline("美图", res);*/
			
			/* 需要引入java-mail的jar包 */
			/*
			 * 如何知道邮件是不是发送成功呢? 只能以是否抛出异常做为判断了
			 */
			this.javaMailSender.send(mimeMessage);
			return true;
		} catch (Exception e)
		{
			ConstatFinalUtil.LOGGER.info("发送邮件失败了;收件人:{},邮件的标题:{}", to, subject, e);
		}
		return false;
	}
	
	public boolean sendHTML_1(String to, String subject, String text)
	{
		ConstatFinalUtil.LOGGER.info("==sendHTML=");
		try
		{
			/* 获取发件人 */
			String from = this.mailMessage.getFrom(); 
			/**
			 * MimeMessagePreparator:接口.
			 * 接口直接new了,后面跟的是匿名类;(只在一个地方使用)
			 */
			MimeMessagePreparator messagePreparator = new MimeMessagePreparator()
			{
				
				@Override
				public void prepare(MimeMessage mailMessage) throws Exception
				{
					/* 写信的过程 */
					mailMessage.setFrom(from);
					/* 设置收件人 */
					mailMessage.setRecipients(RecipientType.TO, to);
					mailMessage.setSubject(subject);
					mailMessage.setText(text);
					mailMessage.setSentDate(new Date());
				}
			};
			

			/* 需要引入java-mail的jar包 */
			/*
			 * 如何知道邮件是不是发送成功呢? 只能以是否抛出异常做为判断了
			 */
			this.javaMailSender.send(messagePreparator);
			return true;
		} catch (Exception e)
		{
			ConstatFinalUtil.LOGGER.info("发送邮件失败了;收件人:{},邮件的标题:{}", to, subject, e);
		}
		return false;
	}
}
